package com.tm.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.behavior.service.ApCollectionService;
import com.tm.media.model.dtos.BehaviorDto;
import com.tm.media.model.entity.ApCollectionEntity;
import com.tm.media.model.mapper.ApCollectionMapper;
import com.tm.media.utils.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author Ly
 * @Data 2023/3/8 19:08
 * @ClassName ApCollectionServiceImpl
 */
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollectionEntity> implements ApCollectionService {
   @Autowired
   private ApCollectionMapper apCollectionMapper;
    @Override
    public Result collection_behavior(BehaviorDto behaviorDto) {
        QueryWrapper<ApCollectionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("behavior_entry_id",1);//假设这个用户的ID为1
        queryWrapper.eq("entry_id",behaviorDto.getEntry_id());
        ApCollectionEntity apCollectionEntity = apCollectionMapper.selectOne(queryWrapper);
        if (apCollectionEntity==null){
            ApCollectionEntity collectionEntity = new ApCollectionEntity();
            collectionEntity.setBehaviorEntryId(1);
            collectionEntity.setEntryId(behaviorDto.getEntry_id());
            collectionEntity.setType(behaviorDto.getType());
            collectionEntity.setCollectionTime(LocalDateTime.now());
            apCollectionMapper.insert(collectionEntity);
        }else {
            apCollectionMapper.deleteById(apCollectionEntity.getId());
        }
        return Result.SUCCESS(200,"收藏成功");
    }
}
